package com.angel.controller;

import com.angel.bean.Employee;
import com.angel.bean.EmployeeType;
import com.angel.bean.SysMenu;
import com.angel.bean.SysRoleMenu;
import com.angel.dtoutlis.MenuDto;
import com.angel.service.EmployeeTypeService;
import com.angel.service.OtherlogService;
import com.angel.service.SysMenuService;
import com.angel.service.SysRoleMenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pro")
public class PromessionController {
    /*
    * 获取员工类型
    * */
    @Autowired
    private EmployeeTypeService employeeTypeService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private OtherlogService otherlogService;


    @RequestMapping(value = "/getemptype")
    @ResponseBody
    public Object GetEmploeeType() {
        EmployeeType employeeType = new EmployeeType();
        employeeType.setState(1);
        List<EmployeeType> employees = employeeTypeService.findEmployeeType(employeeType);
        if (employees == null || employees.isEmpty()) {
            return null;
        }
        return employees;
    }
    /*
    * 插入员工类型id
    * */


    //    @RequestMapping(value = "InsertEmploeeType")
//    @ResponseBody
//    public Object InsertEmploeetype(SysRoleMenu sysRoleMenu){
//        Map<Object, Object> map = new HashMap<Object, Object>();
//        if(sysRoleMenu.getEid()!=null||sysRoleMenu.getMenuId()!=null){
//            List<SysRoleMenu> sysRoleMenuList=sysRoleMenuService.findAll(sysRoleMenu);
//            int suecc=0;
//            if (sysRoleMenuList==null || sysRoleMenuList.isEmpty()){
//              suecc =sysRoleMenuService.insert(sysRoleMenu);
//            }
//            if (suecc==0){
//                map.put("data","失败");
//            }else {
//                map.put("data","失败");
//            }
//       }
//        return map;
//    }
    //查询资源
    @Autowired
    private SysMenuService sysMenuService;

    @RequestMapping(value = "/SelectSysmenu")
    public Object SelectSysmenu(SysMenu sysMenu) {
        List<SysMenu> sysMenuList = sysMenuService.findAll(sysMenu);
        if (sysMenuList == null || sysMenuList.isEmpty()) {
            return null;
        }
        return sysMenuList;
    }

    /*
    * 插入资源Id
    * */
    @RequestMapping(value = "/InsertMeuid")
    @ResponseBody
    public Object InsertEmploeetype(SysRoleMenu sysRoleMenu) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (sysRoleMenu.getEid() != null&&sysRoleMenu.getMenuId()!=null) {
            List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.findAll(sysRoleMenu);
            int sucess = 0;
            if (sysRoleMenuList == null || sysRoleMenuList.isEmpty()) {
                sucess = sysRoleMenuService.insert(sysRoleMenu);
                if (sucess == 0) {
                    map.put("data", "失败");
                } else {
                    map.put("data", "成功");
                }
            } else {
                map.put("data", "权限已存在");
            }

        }
        return map;

    }
    /*
    * 查询权限;
    * */
    @RequestMapping(value = "/SelectSysRole")
    @ResponseBody
    public Object SelectSysRole(@Param("eid")String eid){
        int etid=0;
        try{
            etid=Integer.valueOf(eid);
        }catch (Exception e){

        }
        SysRoleMenu sysRoleMenu=new SysRoleMenu();
        sysRoleMenu.setEid(etid);
        List<SysRoleMenu> sysRoleMenuList=sysRoleMenuService.findAll(sysRoleMenu);
        List<MenuDto> dtoList = new ArrayList<>();
        for (SysRoleMenu sm:sysRoleMenuList){
            SysMenu sysMenu=new SysMenu();
            sysMenu.setId(sm.getMenuId());
            sysMenu=sysMenuService.findOne(sysMenu);
            if (sysMenu!=null){
                dtoList.add(new MenuDto(sysMenu));
            }

        }
//        if (dtoList.isEmpty()){
//            return null;
//        }
        return dtoList;
    }

    @RequestMapping(value = "/DelectRoleMenu")
    @ResponseBody
    public Object  Delect(SysRoleMenu sysRoleMenu){
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (sysRoleMenu.getEid()!=null||sysRoleMenu.getMenuId()!=null){
            int sucess=0;
            sucess= sysRoleMenuService.delSysRoleMenu(sysRoleMenu);
            if (sucess == 0) {
                map.put("data", "失败");
            } else {
                map.put("data", "成功");
            }
        }
        return map;
    }
}
